package csc.daonjpa.java.controller;

import java.util.List;

import javax.persistence.PersistenceException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import org.apache.log4j.Logger;
import org.hibernate.exception.ConstraintViolationException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;
import csc.daonjpa.java.domain.Card;
import csc.daonjpa.java.domain.Customer;
import csc.daonjpa.java.service.CardService;
import csc.daonjpa.java.service.CustomerService;

@Controller
public class CardController {
	private static final Logger LOG = Logger.getLogger(AccountController.class);
	@Autowired
	private CustomerService customerService;
	
	@Autowired
	private CardService cardService;
	

	@RequestMapping(value = "/card")
	public String registerAccountPage() {
		return "forward:/listCard";
	}
	/**
	 * add card to database
	 * @param session
	 * @param request
	 * @param model
	 * @return
	 */
	@RequestMapping(value = "/createCard")
	public String addCard(HttpSession session, HttpServletRequest request, Model model) {
		// Read account info from request and save into Card object
		
		String idLogin = session.getAttribute("loginSession").toString();
		String cardType = request.getParameter("cardType");
		Double availableAmount = Double.parseDouble(request.getParameter("availableAmount"));
		Customer customer = customerService.getCustomer(idLogin);
		Card card = new Card(availableAmount, cardType, customer);
		try {
			// Save account to DB
			cardService.addCard(card);
			model.addAttribute("successMessage",
					"Added new card successfully");
		} catch (PersistenceException ex) {
			if (ex.getCause() instanceof ConstraintViolationException) {
				model.addAttribute("errorMessage", "Duplicated account found");
			} else {
				model.addAttribute("errorMessage", "Error adding new card");
			}
			LOG.error("Error found adding an card", ex);
			throw ex;
		} catch (Exception e) {
			LOG.error("General Error found adding an card", e);
			throw e;
		}

		return "forward:/listCard";
	}
	/**
	 * show cards of user logged in
	 * @param session
	 * @param model
	 * @return
	 */
	@RequestMapping(value = "/listCard")
	public String getAccountList(HttpSession session, Model model) {
		String loginId = session.getAttribute("loginSession").toString();
		// Get the list of all cards from DB
		List<Card> cardList = cardService.getCardByCustomer(loginId);
		model.addAttribute("listCard", cardList);
		return "listCard";
	}

	@ExceptionHandler(Exception.class)
	public ModelAndView handleAllException(Exception ex) {

		ModelAndView model = new ModelAndView("listCard");
		model.addObject("errorMessage", "Exception occurred");

		return model;
	}
}
